package com.itheima.service.impl.pms;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.core.PageInfo;
import com.itheima.mapper.pms.PmsProductCategoryMapper;
import com.itheima.pojo.pms.PmsProduct;
import com.itheima.pojo.pms.PmsProductCategory;
import com.itheima.pojo.pms.vo.PmsProductCategoryVo;
import com.itheima.pojo.pms.vo.PmsProductCategoryWithChildrenItemVo;
import com.itheima.service.pms.PmsProductCategoryService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
public class PmsProductCategoryServiceImpl extends ServiceImpl<PmsProductCategoryMapper, PmsProductCategory>implements PmsProductCategoryService {

    @Autowired
    private PmsProductCategoryMapper productCategoryMapper;

    @Override
    public List<PmsProductCategory> listWithChildren() {
        List<PmsProductCategory> productCategoryList = productCategoryMapper.findByParentId();
        return productCategoryList.stream()
                .map((item) -> {
                    item.setChildren(getChild(item.getId()));
                    return item;
                }).collect(Collectors.toList());
    };
    private List<PmsProductCategory> getChild(Long id) {
        List<PmsProductCategory>productCategoryLists = productCategoryMapper.findById(id);
        productCategoryLists.stream().map((item) -> {
            item.setChildren(getChild(item.getId()));
            return item;
        }).collect(Collectors.toList());
        return productCategoryLists;
    }



    @Override
    public void updateShowStatus(List<Long> ids, int showStatus) {
        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PmsProductCategory::getId,ids);
        PmsProductCategory ppc = new PmsProductCategory();
        ppc.setShowStatus(showStatus);
        this.update(ppc,queryWrapper);
    }

    @Override
    public void updateNavStatus(List<Long> ids, int navStatus) {
        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PmsProductCategory::getId,ids);
        PmsProductCategory ppc = new PmsProductCategory();
        ppc.setNavStatus(navStatus);
        this.update(ppc,queryWrapper);
    }

    @Override
    public void updateProductCategory(Long id, PmsProductCategoryVo pmsProductCategoryVo) {
        LambdaQueryWrapper<PmsProductCategory> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(id != null,PmsProductCategory::getId,id);
        PmsProductCategory one = this.getOne(queryWrapper);
        BeanUtils.copyProperties(pmsProductCategoryVo,one,"productAttributeIdList","id");

        LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsProduct::getProductCategoryName,one.getName());
        lqw.eq(PmsProduct::getProductCategoryId,id);
        this.update(one,queryWrapper);
    }

    /**
     * 分页查询商品分类
     * @param parentId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo listByPage(Long parentId, Integer pageNum, Integer pageSize) {
        Page<PmsProductCategory> categoryPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsProductCategory> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(PmsProductCategory::getParentId,parentId);
        Page<PmsProductCategory> page = this.page(categoryPage, categoryLambdaQueryWrapper);
        PageInfo pageInfo = new PageInfo();
        pageInfo.setList(page.getRecords());
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotalPage((int) page.getCurrent());
        pageInfo.setTotal((int) page.getTotal());
        return pageInfo;
    }
}
